package com.zmn.mcc.admin.controller.module;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.mcc.model.entity.module.McModule;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/15 10:46
 */
@Controller
@RequestMapping("/module/modify/")
public class ModuleModifyController extends ModuleBaseController {


    /**
     * 新增/修改
     */
    @RequestMapping("addEdit")
    @ResponseBody
    public ResponseDTO addEdit(HttpServletRequest request, HttpServletResponse response,
                               @RequestBody McModule module) {

        module.setUpdater(getStaffName());
        if(NumberUtil.isNullOrZero(module.getModuleId())){
            return moduleBService.addModule(module);
        } else {
            return moduleBService.modifyModule(module);
        }
    }

    /**
     * 修改可用状态
     */
    @RequestMapping("modifyStatus")
    @ResponseBody
    public ResponseDTO modifyStatus(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(value = "ids", defaultValue = "") String ids,
                                  @RequestParam(value = "status", defaultValue = "0") Integer status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的模块");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        moduleService.updateStatusBatch(arrayIds, status, getStaffName());
        return ResponseDTO.success(null, "操作成功");
    }

    /**
     * 修改排序值
     */
    @RequestMapping("modifySort")
    @ResponseBody
    public ResponseDTO modifySort(HttpServletRequest request, HttpServletResponse response,
                                @RequestParam(value = "id", defaultValue = "0") Integer id,
                                @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (NumberUtil.isNullOrZero(id)) {
            return ResponseDTO.fail("请选择要更改排序的模块");
        }
        if (NumberUtil.isNullOrZero(sort)) {
            return ResponseDTO.fail("请设置模块排序值");
        }

        moduleService.updateSort(id, sort);
        return ResponseDTO.success(null, "操作成功");
    }
}
